package com.cm.sys.system.service.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cm.sys.framework.application.ApiResponseCode;
import com.cm.sys.framework.execption.BusinessException;
import com.cm.sys.framework.utils.DateUtil;
import com.cm.sys.framework.utils.JsonUtil;
import com.cm.sys.framework.utils.PageList;
import com.cm.sys.system.dao.RoleMapper;
import com.cm.sys.system.entity.Role;
import com.cm.sys.system.service.RoleService;

/***
 * 用户模块
 * 
 * @author yxq18
 *
 */
@Service
public class RoleServiceImpl implements RoleService {

    private static final Logger logger = LoggerFactory.getLogger(RoleServiceImpl.class);

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PageList pageList;

    @Override
    public Role saveRole(JSONObject args) {
        logger.info("角色保存的入参==================【{}】===============", args);

        String roleName = args.getString("roleName");
        String roleDescrpt = args.getString("roleDescrpt");
        Role userRole = new Role();
        userRole.setCreateTime(DateUtil.getTimestamp());
        userRole.setRoleName(roleName);
        userRole.setRoleDescrpt(roleDescrpt);
        userRole.setUpdateTime(DateUtil.getTimestamp());

        logger.info("角色保存对象的入参==================【{}】===============", JSON.toJSONString(userRole));
        roleMapper.insert(userRole);
        logger.info("用户保存返回的值==================【{}】===============", JSON.toJSONString(userRole));
        return userRole;
    }

    @Override
    public int delRole(JSONObject args, Integer roleId) {
        logger.info("角色删除的入参==================【{}】===============", args);

        int count = 0;
        Role userRole = selectRole(roleId);
        if (!StringUtils.isEmpty(userRole)) {
            count = roleMapper.deleteByPrimaryKey(userRole.getRoleId());
        }

        return count;
    }

    @Override
    public Role updateRole(JSONObject args, Integer roleId) {

        logger.info("角色更新的入参==================【{}】===============", args);

        String roleName = JsonUtil.getString(args, "roleName", "");
        String roleDescrpt = JsonUtil.getString(args, "roleDescrpt", "");

        Role userRole = selectRole(roleId);
        if (StringUtils.isEmpty(userRole)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), "所传roleId查不到角色信息");
        }
        if (!StringUtils.isEmpty(roleName)) {
            userRole.setRoleName(roleName);
        }
        if (!StringUtils.isEmpty(roleDescrpt)) {
            userRole.setRoleDescrpt(roleDescrpt);
        }

        userRole.setUpdateTime(DateUtil.getTimestamp());
        logger.info("角色对象更新的入参==================【{}】===============", JSON.toJSONString(userRole));
        roleMapper.updateByPrimaryKey(userRole);
        return userRole;
    }

    @Override
    public List<Role> selectRoles(JSONObject args) {

        logger.info("角色列表查询的入参==================【{}】===============", args);

        String search = JsonUtil.getString(args, "search", "");
        List<Role> list = roleMapper.selectAll(search);

        logger.info("角色列表查询的返回值==================【{}】===============", list);
        return list;
    }

    @Override
    public Role selectRole(Integer roleId) {

        Role userRole = new Role();
        if (StringUtils.isEmpty(roleId)) {
            throw new BusinessException(ApiResponseCode.PARAM_NEED.getCode(), "所传参数为空");
        } else {
            userRole = roleMapper.selectByPrimaryKey(roleId);
        }
        return userRole;
    }
}
